<?php

	class EditBoutiqueCommand implements ICommand
	{
		public function execute($event)
		{
			$name 		= $event->data['name'];
			$urlname 	= EbUrlRoutingToolkit::formatToUrl($name);
			$model 		= EbModel::getModel('VarPeople','Boutique');
			$buffer 	= $model->selectByName($name);
			$newYN 		= !isset($event->data['bo_id']);
						
			if ($newYN && count($buffer) > 0)
			{
				return new Event('redirect','/admin-boutique');
			}
			
			$buffer = new StdClass();
			
			$buffer->name 		= $event->data['name'];
			$buffer->urlname 	= $urlname;
			$buffer->ca_id 		= $event->data['ca_id'];
			$buffer->description = $event->data['description'];
			$buffer->adress 	= $event->data['adress'];
			$buffer->activity 	= $event->data['activity'];
			$buffer->contact 	= $event->data['contact'];
			$buffer->city 		= $event->data['city'];
			
			$buffer->hits		= 0;
			
			if ($newYN)
			{
				// Create new media folder
				$mediafolder_id = EbModel::getModel('Media','MediaFolder')->insert((object) array('name' => $buffer->name,'pr_id' => 2));
				
				$buffer->mediafolder_id = $mediafolder_id;
				
				// Get lon & lat
				$geocoding_event 	= new Event('GoogleMaps/GetGeocoding',$buffer->adress.','.$buffer->city);
				$geocoding_result	= $geocoding_event->dispatch();
				
				if (!is_null($geocoding_result))
				{
					$buffer->lon	= $geocoding_result->lon;
					$buffer->lat	= $geocoding_result->lat;
				}
				else
				{
					$buffer->lon	= '5.935482416967766';
					$buffer->lat	= '43.12649062486129';
				}
								
				$model->insert($buffer);
			}
			else
			{
				$buffer->bo_id = $event->data['bo_id'];
				
				$model->update($buffer);
			}
			
			return new Event('redirect','/admin-boutique');
				
		}		
	}